/*
* 数据缓存管理
* */
import 'dart:convert';
import 'dart:developer';
import 'package:lighthouse/index.dart';

/*用户配置缓存*/
class UserConfigureCache {
  static const String _keyStr = "userConfigure";

  // 单例公开访问点
  factory UserConfigureCache() => _sharedInstance();

  // 静态私有成员，没有初始化
  static UserConfigureCache? _instance;

  // 私有构造函数
  UserConfigureCache._() {
    // 具体初始化代码
  }

  // 静态、同步、私有访问点
  static UserConfigureCache _sharedInstance() {
    return _instance ??= UserConfigureCache._();
  }

  late bool _isLoadData = false;

  /*当前网络数据*/
  UserConfigure? _netData;

  /*历史缓存数据*/
  UserConfigure? _cacheData;

  /*加载网络数据*/
  Future loadNetData(BuildContext context) async {
    if (!_isLoadData) {
      _isLoadData = true;
      try {
        ResponseData<UserConfigure> responseData =
            await Api(context).getUserConfigure();
        //保存数据
        if (responseData.success && responseData.dataModel != null) {
          _netData = responseData.dataModel;
          Global.getPrefs()
              .setString(_keyStr, responseData.data["homeSetting"]);
        }
      } catch (e) {
        Debug.info(e.toString());
        _isLoadData = false;
        if (e is DioError) {
          //登录失败则提示
          showToast(e.response?.statusMessage ?? S.of(context).networkError);
        } else if (e is Error) {
          Debug.info(e.stackTrace.toString());
        }
      } finally {
        // 隐藏loading框
        // Navigator.of(context).pop();
      }
    }
    return _isLoadData;
  }

  /*获取当前数据*/
  UserConfigure? getCurrentData() {
    UserConfigure? mData;
    if (_netData != null) {
      // 如果有请求的数据 就返回请求的数据
      mData = _netData;
    } else if (_cacheData == null) {
      // 设置缓存数据
      var _cacheStr = Global.getPrefs().getString(_keyStr);
      if (_cacheStr != null) {
        try {
          mData = _cacheData = UserConfigure.fromJson(jsonDecode(_cacheStr));
        } on Error catch (e) {
          Debug.info(e.toString());
          Debug.info(e.stackTrace.toString());
        }
      }
    } else {
      mData = _cacheData;
    }
    return mData;
  }

  Future<bool> updateUserConfigure(
      BuildContext context, UserConfigure userConfigure) async {
    showLoading(context, text: "保存中");
    try {
      ResponseData responseData =
          await Api(context).updateUserConfigure(userConfigure);
      //保存数据

      if (responseData.success) {
        Global.getPrefs()
            .setString(_keyStr, jsonEncode(userConfigure.toJson()));
        return true;
      } else {
        return false;
      }
    } catch (e) {
      if (e is DioError) {
        //登录失败则提示
        showToast(e.response?.statusMessage ?? S.of(context).networkError);
      } else if (e is Error) {
        Debug.info(e.stackTrace.toString());
      }
      return false;
    } finally {
      // 隐藏loading框
      Navigator.of(context).pop();
    }
  }

  /*清楚数据*/
  void clearData() {
    _isLoadData = false;
    _netData = null;
    _cacheData = null;
    Global.getPrefs().remove(_keyStr);
  }
}
